package com.ctgu.AutoIdentify.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctgu.AutoIdentify.DTO.PredictDTO;
import com.ctgu.AutoIdentify.common.Result;
import com.ctgu.AutoIdentify.entity.Record;
import com.ctgu.AutoIdentify.mapper.RecordMapper;
import com.ctgu.AutoIdentify.service.IRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/predict")
public class PredictController {
    @Autowired
    private IRecordService recordService;

    private RecordMapper recordMapper;

    @CrossOrigin(origins = "*")
    @PostMapping("/feature")
    public int predict(@RequestBody PredictDTO predictDTO){//接收前端特征值，然后发送给flask应用调用机器学习模型得到预测结果最后返回
        return  recordService.predict(predictDTO);
    }

    @CrossOrigin(origins = "*")
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam("userid") String userid){
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(userid)) {
            queryWrapper.like("userid", userid);
        }

        return Result.success(recordService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @CrossOrigin(origins = "*")
    @PostMapping("/add")
    public Result save(@RequestBody Record record){
        //新增或者更新
        return Result.success(recordService.saveOrUpdate(record));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(recordService.removeById(id));
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(recordService.removeByIds(ids));
    }
}
